<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Enum 枚举</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.types.object.html">« Object 对象</a></li>
      <li style="float: right;"><a href="language.types.resource.html">Resource 资源类型 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.types.html">类型</a></li>
    <li>Enum 枚举</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.types.enumerations" class="sect1">
 <h2 class="title">Enum 枚举</h2>

 <div class="sect2" id="language.types.enumerations.basics">
  <h3 class="title">枚举基础</h3>

  <p class="para">
   枚举是在类、类常量基础上的约束层，
   目标是提供一种能力：定义包含可能值的封闭集合类型。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />enum&nbsp;Suit<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Hearts</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Diamonds</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Clubs</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Spades</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">do_stuff</span><span style="color: #007700">(</span><span style="color: #0000BB">Suit&nbsp;$s</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">do_stuff</span><span style="color: #007700">(</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Spades</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="simpara">
   详情参阅<a href="language.enumerations.html" class="link">枚举</a>章节。
  </p>

 </div>
 
 <div class="sect2" id="language.types.enumerations.casting">
  <h3 class="title">类型转换</h3>
  
  <p class="para">
   将 <span class="type"><span class="type enum">enum</span></span> 转换为 <span class="type">object</span> 不会有变化。
   将 <span class="type"><span class="type enum">enum</span></span> 转换为 <span class="type">array</span>，
   纯粹枚举会创建单个 <code class="literal">name</code> 键的数组；
   回退枚举创建带 <code class="literal">name</code> 和 <code class="literal">value</code> 键的数组。
   其他类型转换都会导致错误。
  </p>
 </div>
</div></div></div></body></html>